#+
#
#-
import os
import glob
import tarfile

# Use barras duplas "\\" se estiver usando windows.
inputDir  = "G:\\TEMP\\PROCESSAR"
outputDir = "G:\\TEMP\\PROCESSAR"

tarGzFileList = glob.glob(inputDir +'\\*.tar.gz')

for tarGzFile in tarGzFileList:
    splited = tarGzFile.split("\\")
    splited = splited[len(splited)-1].split(".")
    
    folderName = splited[0]
    
    untaredFilesDir = outputDir +'\\'+ folderName
    if not os.path.exists(untaredFilesDir):
        os.makedirs(untaredFilesDir)
    
    os.chdir(untaredFilesDir)
    
    print 'Uncompressing file...' + tarGzFile
    try:
        tar = tarfile.TarFile.open(tarGzFile, 'r')
        tar.extractall()
        tar.close()
    except:
            print "The file cannot be uncompressed! "  + tarGzFile

print "Finished!"